package com.uuticketweb.ticket.user.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.bind.ServletRequestUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.uuticketweb.ticket.db.HibernateUtils;
import com.uuticketweb.ticket.flight.service.factory.OrderServiceFactory;
import com.uuticketweb.ticket.pojos.Order;
import com.uuticketweb.ticket.pojos.UserDetail;
import com.uuticketweb.ticket.user.service.UserService;
import com.uuticketweb.ticket.user.service.factory.UserServiceFactory;

public class UserAction extends ActionSupport {
	private UserDetail user;
	
	public String searchUser() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		int type = ServletRequestUtils.getIntParameter(request, "type", UserDetail.TYPE_ADMIN);
		if(type < UserDetail.TYPE_ADMIN || type > UserDetail.TYPE_STAFF) {
			type = UserDetail.TYPE_ADMIN;
		}
		String query = ServletRequestUtils.getStringParameter(request, "query");
		UserService userService = UserServiceFactory.getUserService();
		List userList = userService.searchUser(type, query);
		
		ActionContext.getContext().put("userList", userList);
		ActionContext.getContext().put("type", type);
		return "searchUser";
	}
	
	public String showStaffArea() throws Exception {
		String strId = null;
		if(ActionContext.getContext().getParameters().get("userId") != null) {
			strId = ((String[])ActionContext.getContext().getParameters().get("userId"))[0];
		}
		user = null;
		if(strId != null) {
			user = getUserDetail(strId);
		}
		if(user != null) {
			if(String.valueOf(UserDetail.TYPE_STAFF).equals(user.getType())) {
				String area = user.getArea();
				String[] allArea = new String[]{UserDetail.AREA_ASIA, UserDetail.AREA_AMER, UserDetail.AREA_EURO, UserDetail.AREA_AFRI};
				boolean[] hasArea = new boolean[allArea.length];
				if(!StringUtils.isBlank(area)) {
					for(int i = 0; i < allArea.length; i ++) {
						if(area.indexOf("*" + allArea[i] + "*") != -1) {
							hasArea[i] = true;
						}
					}
				}
				ActionContext.getContext().put("user", user);
				ActionContext.getContext().put("hasArea", hasArea);
			}
		}
		return "staffArea";
	}
	
	public String saveStaffArea() throws Exception {
		String strId = null;
		if(ActionContext.getContext().getParameters().get("userId") != null) {
			strId = ((String[])ActionContext.getContext().getParameters().get("userId"))[0];
		}
		user = null;
		if(strId != null) {
			user = getUserDetail(strId);
		}
		if(user != null) {
			Map para = ActionContext.getContext().getParameters();
			String[] area = (String[])para.get("area");
			if(area != null && area.length != 0) {
				String strarea = "*";
				for(int i = 0; i < area.length; i ++) {
					strarea += area[i] + "*";
				}
				user.setArea(strarea);
			}
			HibernateUtils.save(user);
			ActionContext.getContext().put("user", user);
			return "saveStaffArea";
		}
		return "listStaff";
	}
	

	private UserDetail getUserDetail(String id) throws Exception {
		return (UserDetail)OrderServiceFactory.getOrderService().getPojo(Long.parseLong(id), UserDetail.class);
	}

	public UserDetail getUser() {
		return user;
	}

	public void setUser(UserDetail user) {
		this.user = user;
	}
	
	
}
